package net.sf.rose.redis;

import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import net.sf.rose.util.RoseRuntimeException;
import net.sf.rose.util.StringUtil;
import redis.clients.jedis.Jedis;

/** 
 * @author fengjian E-mail: 9110530@qq.com 
 * @version 创建时间：2018年6月13日
 * 类说明：封装了Redis的客户端部分功能
 */
@Component
public class RedisClient {

	private static Logger LOG = Logger.getLogger(RedisClient.class);

	/**
	 * 往缓存里增加数据
	 * @param key
	 * @param val
	 */
	public void set(String key, Object val) {
		Jedis jedis = RedisPoolUtils.getJedis();
		try {
			String str = val instanceof String ? val.toString() : StringUtil.render(val);
			jedis.set(key, str);
		} catch (Exception e) {
			LOG.error(e);
			throw new RoseRuntimeException(e.getMessage());
		} finally {
			jedis.close();
		}
	}

	/**
	 * 往缓存里增加数据
	 * @param key
	 * @param seconds
	 * @param val
	 */
	public void set(String key, int seconds, Object val) {
		Jedis jedis = RedisPoolUtils.getJedis();
		try {
			String str = val instanceof String ? val.toString() : StringUtil.render(val);
			jedis.setex(key, seconds, str);
		} catch (Exception e) {
			LOG.error(e);
			throw new RoseRuntimeException(e.getMessage());
		} finally {
			jedis.close();
		}
	}

	/**
	 * 从缓存里获取数据
	 * @param key
	 */
	public String get(String key) {
		Jedis jedis = RedisPoolUtils.getJedis();
		try {
			return jedis.get(key);
		} catch (Exception e) {
			LOG.error(e);
			throw new RoseRuntimeException(e.getMessage());
		} finally {
			jedis.close();
		}
	}

	/**
	 * 从缓存里获取数据对象
	 * @param key
	 */
	@SuppressWarnings("unchecked")
	public <T> T get(Class<?> c, String key) {
		String str = get(key);
		if (str == null) {
			return null;
		}
		return (T) StringUtil.parseJson(c, str);
	}

	/**
	 * 往缓存里增加数据
	 * @param key
	 * @param val
	 */
	public void set(String sort, String key, Object val) {
		Jedis jedis = RedisPoolUtils.getJedis();
		try {
			String str = val instanceof String ? val.toString() : StringUtil.render(val);
			jedis.hset(sort, key, str);
		} catch (Exception e) {
			LOG.error(e);
			throw new RoseRuntimeException(e.getMessage());
		} finally {
			jedis.close();
		}
	}

	/**
	 * 从缓存里获取数据
	 * @param key
	 */
	public String get(String sort, String key) {
		Jedis jedis = RedisPoolUtils.getJedis();
		try {
			return jedis.hget(sort, key);
		} catch (Exception e) {
			LOG.error(e);
			throw new RoseRuntimeException(e.getMessage());
		} finally {
			jedis.close();
		}
	}

	/**
	 * 从缓存里获取数据对象
	 * @param key
	 */
	@SuppressWarnings("unchecked")
	public <T> T get(Class<?> c, String sort, String key) {
		String str = get(sort, key);
		if (str == null) {
			return null;
		}
		return (T) StringUtil.parseJson(c, str);
	}

	/**
	 * 删除缓存数据
	 * @param key
	 */
	public void hdelAll(String sort) {
		Jedis jedis = RedisPoolUtils.getJedis();
		try {
			Set<String> set = jedis.hkeys(sort);
			String[] arr = new String[set.size()];
			int i = 0;
			for (String field : set) {
				arr[i] = field;
				i++;
			}
			jedis.hdel(sort, arr);
		} catch (Exception e) {
			LOG.error(e);
			throw new RoseRuntimeException(e.getMessage());
		} finally {
			jedis.close();
		}
	}

	/**
	 * 发布消息订阅
	 * @param channel
	 * @param message
	 */
	public void publish(String channel, String message) {
		Jedis jedis = RedisPoolUtils.getJedis();
		try {
			jedis.publish(channel, message);
		} catch (Exception e) {
			LOG.error(e);
			throw new RoseRuntimeException(e.getMessage());
		} finally {
			jedis.close();
		}
	}
}
